package org.example.main.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.main.entity.Admin;
import org.example.main.utils.CommonResult;
import org.example.main.entity.Favorite;
import org.example.main.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@CrossOrigin
@Tag(name = "收藏管理",description = "收藏管理相关接口")
@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    @Operation(summary = "添加收藏")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加成功"),
            @ApiResponse(responseCode = "500", description = "添加失败"),
            @ApiResponse(responseCode = "400", description = "已收藏"),
    })
    @PostMapping("/add")//http://localhost:8089/main/favorite/add
    public CommonResult add(@RequestBody Favorite favorite) {
        CommonResult commonResult = new CommonResult();
        try {
            Favorite existing = favoriteService.getByTenantAndHouse(favorite.getTenantId(), favorite.getHouseId());
            if (existing != null) {
                commonResult.setCode(400);
                commonResult.setMsg("已收藏");
            } else {
                favorite.setFavoriteTime(new Date());
                favoriteService.add(favorite);
                commonResult.setCode(200);
                commonResult.setMsg("收藏成功");
            }
        }catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("收藏失败");
        }
        return commonResult;
    }

    @Operation(summary = "取消收藏")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "取消成功"),
            @ApiResponse(responseCode = "500", description = "取消失败")
    })
    @DeleteMapping("/cancel")//http://localhost:8089/main/favorite/cancel
    public CommonResult cancel(@RequestBody Favorite favorite) {
        CommonResult commonResult = new CommonResult();
        try{
            long tenantId = favorite.getTenantId();
            long houseId = favorite.getHouseId();
            favoriteService.removeByTenantAndHouse(tenantId, houseId);
            commonResult.setCode(200);
            commonResult.setMsg("取消成功");
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("取消失败");
        }
        return commonResult;
    }

    @Operation(summary = "根据租客ID查询收藏")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/tenant/{tenantId}")//http://localhost:8089/main/favorite/tenant/{tenantId}
    public CommonResult getByTenant(@PathVariable long tenantId) {
        CommonResult commonResult = new CommonResult();
        try{
            List<Favorite> list = favoriteService.getByTenantId(tenantId);
            if(!list.isEmpty()) {
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(list);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "根据房源ID查询收藏")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/house/{houseId}")//http://localhost:8089/main/favorite/house/{houseId}
    public CommonResult getByHouse(@PathVariable long houseId) {
        CommonResult commonResult = new CommonResult();
        try{
            List<Favorite> list = favoriteService.getByHouseId(houseId);
            commonResult.setCode(200);
            commonResult.setMsg("查询成功");
            commonResult.setData(list);
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "查询所有")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/findAll")//http://localhost:8089/main/favorite/findAll
    public CommonResult findAll() {
        CommonResult commonResult = new CommonResult();
        try{
            List<Favorite> favorites = favoriteService.findAll();
            if(!favorites.isEmpty()) {
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(favorites);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "根据租客&房源查询收藏")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @PostMapping("/tenantAndHouse")//http://localhost:8089/main/favorite/tenantAndHouse
    public CommonResult getByTenantAndHouse(@RequestBody Favorite favorite) {
        CommonResult commonResult = new CommonResult();
        try{
            Favorite existing = favoriteService.getByTenantAndHouse(favorite.getTenantId(), favorite.getHouseId());
            if (existing != null) {
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(true);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
                commonResult.setData(false);
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }
}

